package com.appkubes.mall.admin.application.user.user.controller;


import com.appkubes.common.Response;
import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.admin.application.base.BaseController;
import com.appkubes.mall.admin.application.user.user.entity.model.GetListResult;
import com.appkubes.mall.admin.application.user.user.service.impl.UserServiceImpl;
import com.appkubes.mall.admin.utils.PageUtil;
import com.appkubes.mall.core.entity.MallAdmin;
import com.appkubes.mall.core.entity.MallUser;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Api("用户模块")
@RestController
@RequestMapping("/sass_admin/Suser")
public class SaaSAdminController extends BaseController {

    @Autowired
    UserServiceImpl userServiceImpl;


    @ApiOperation(value = "用户列表")
    @PostMapping("/getList")
    public Response getList(@RequestParam(defaultValue = "") String key,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "20") Integer limit,
                            @RequestParam(defaultValue = "false") Boolean isSuperPlat) {
        String appKey = null;
        if (!isSuperPlat) {
            appKey = getAppKey();
        }
        PageInfo<GetListResult> pageinfo = userServiceImpl.getList(appKey, key, page, limit);
        return Response.success(new PageUtil<GetListResult>(pageinfo));
    }

    @ApiOperation("注册新用户")
    @PostMapping("/registerNewUser")
    public Response registerNewUser(MallUser newUser) {
        if (StringUtils.isEmpty(newUser.getTel())) {
            return Response.error("新建失败,手机号码不可为空");
        }
        MallUser user = userServiceImpl.getUserByTel(newUser.getTel());
        if (user != null) {
            return Response.error("新建失败,用户已存在");
        }
        newUser.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        newUser.setLoginTime(DateUtils.DateToTimestamp(new Date()));
        newUser.setAppKey(null);
        newUser.setCreateTime(DateUtils.getCurrentTimestamp());
        userServiceImpl.save(newUser);
        return Response.success(newUser);
    }
    @ApiOperation("保存商品信息个人信息")
    @PostMapping("/modify")
    public Response modify(MallAdmin mallAdmin){
        System.out.println(mallAdmin.getAdminKey()+"就喀喀喀"+mallAdmin.getName());
        int result = userServiceImpl.modify(mallAdmin);
        System.out.println("返回结果"+result);
        return Response.success(result);
    }


}
